<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>非受控组件</title>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="../js/prop-types.js"></script>
    <style>
        
    </style>

</head>
<body>
    <div id="test"></div>
    <script type="text/babel">
        class Login extends React.Component{
            // 初始化状态
            state={
                username:"",
                password:""
            }
            render(){
                return(
                    <form onSubmit={this.handleLogin}>
                        用户名：<input type="text" onChange={this.saveFormData('username')}/><br />
                        密码：<input type="password" onChange={this.saveFormData('password')}/><br/>
                        <button>登录</button>
                    </form>
                )
            }
            // 保存用户信息到state中
            // 高阶函数和函数柯里化
            saveFormData=(type)=>{
                return (e)=>{
                    this.setState({[type]:e.target.value})
                }
            }
            handleLogin=(e)=>{
                e.preventDefault()
                const {username,password} =this.state
                alert(`用户名是${username}，密码是${password}`)
            }
        }
        ReactDOM.render(<Login/>,document.getElementById('test'))
    </script>
    
</body>
</html>